package com.xxx.controller;

import com.xxx.common.Result;
import com.xxx.common.ResultUtil;
import com.xxx.eneity.User;
import com.xxx.service.UserService;
import com.xxx.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/login")
    public Result userLogin(@RequestParam String username, @RequestParam String password) {
        User user = userService.getUser(username, password);
        if (user != null) {
            return ResultUtil.success(user);
        } else {
            return ResultUtil.userNotExist();
        }
    }

    @GetMapping("/isUserExist")
    public Result isUserExist(@RequestParam String username) {
        boolean userExist = userService.isUserExist(username);
        if (userExist) {
            return ResultUtil.error(1003, "用户已存在");
        } else {
            return ResultUtil.success("该用户名可用");
        }
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (user.getUsername() == null || "".equals(user.getUsername())) {
            return ResultUtil.error(1005, "用户名不能为空");
        } else if (user.getPassword() == null) {
            return ResultUtil.error(1006, "密码不能为空");
        } else if (user.getName() == null || "".equals(user.getName())) {
            return ResultUtil.error(1098, "姓名不能为空");
        } else if (user.getCode() == null || user.getCode() == 0) {
            return ResultUtil.error(1097, "学号/工号不能为空");
        }
        boolean codeExist = userService.isCodeExist(user.getCode());
        if (!codeExist) {
            return ResultUtil.error(1096, "学号/工号不存在");
        }
        boolean codeRegistered = userService.isCodeRegistered(user.getCode());
        if (codeRegistered) {
            return ResultUtil.error(1095, "学号/工号已注册");
        }
        Integer role = userService.getRole(user.getCode());
        user.setRole(role);
        boolean userExist = userService.isUserExist(user.getUsername());
        if (userExist) {
            return ResultUtil.error(1003, "用户已存在");
        }
        int register = userService.register(user);
        if (register > 0) {
            return ResultUtil.success("注册成功");
        } else {
            return ResultUtil.error(1004, "注册失败");
        }
    }

    @PostMapping("/editPassword")
    public Result editPassword(Integer userId, String oldpwd, String newpwd) {
        // 判断密码是否正确
        int i = userService.getCount(userId, oldpwd);
        if (i <= 0) {
            return ResultUtil.error(2004, "旧密码错误");
        }
        int result = userService.editPassword(userId, newpwd);
        if (result > 0) {
            return ResultUtil.success("修改密码成功");
        } else {
            return ResultUtil.error(2005, "修改密码失败");
        }
    }

    @PostMapping("/editUser")
    public Result editUser(@RequestBody User user) {
        int i = userService.editUser(user);
        if (i > 0) {
            return ResultUtil.success("修改用户信息成功");
        } else {
            return ResultUtil.error(2006, "修改用户信息失败");
        }
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam Integer userId) {
        User user = userService.getUserInfo(userId);
        return ResultUtil.success(user);
    }

    @GetMapping("/getUserList")
    public Result getUserList() {
        List<UserVo> userVoList = userService.getUserList();
        return ResultUtil.success(userVoList);
    }
}
